package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.mapper.QuestionMapper;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.QuestionQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/7/19.
 */
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void delete(Long id) {
        questionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Question question) {
        if (question.getId() == null) {
            question.setCreatetime(new Date());
            questionMapper.insert(question);
        } else {
            questionMapper.updateByPrimaryKey(question);
        }
    }

    @Override
    public List<Question> listAll() {
        return questionMapper.selectAll();
    }

    @Override
    public Question get(Long id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo query(QuestionQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List list = questionMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void changeState(Question question) {
            question.setReleasetime(new Date());
            questionMapper.updateStateById(question);
        }
}
